package com.isoftstone.demo.tree.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;

import com.isoftstone.demo.tree.entity.TreeNodeData;
import com.isoftstone.demo.tree.repository.DemoTreeDao;
import com.isoftstone.demo.tree.service.DemoTreeService;
import com.isoftstone.services.base.vo.TreeNode;
import com.isoftstone.services.data.service.impl.BaseJpaService;

/**
 * 
 * <br> Demo树的Service
 * @author  tulinli
 * @version  [版本号, 2017年11月23日]
 * @see  [相关类/方法]
 * @since  [产品/模块版本]
 */
@Service
public class DemoTreeServiceImpl
    extends BaseJpaService<TreeNodeData, Integer, DemoTreeDao>
    implements DemoTreeService
{
    
    @Override
    public List<TreeNode> tree(String lang, Integer parentId, Integer level)
    {
        
        List<TreeNode> nodes = new ArrayList<>();
        // 根节点ID
        // long dictParentId = Integer.MAX_VALUE - 1;
        if (parentId == null)
        {
            
            List<TreeNodeData> treeNodeData = dao.findAllByPIdIsNull();
            for (TreeNodeData nodeData : treeNodeData)
            {
                TreeNode node = new TreeNode();
                node.setId(nodeData.getId().longValue());
                // node.setpId(dictParentId);
                node.setName(nodeData.getName());
                node.setIsParent(Boolean.TRUE);
                node.setType(nodeData.getNodeType());
                nodes.add(node);
            }
        }
        else
        {
            List<TreeNodeData> treeNodeData = dao.findAllByPId(parentId);
            for (TreeNodeData nodeData : treeNodeData)
            {
                TreeNode node = new TreeNode();
                node.setId(nodeData.getId().longValue());
                node.setpId(parentId.longValue());
                node.setName(nodeData.getName());
                node.setIsParent(nodeData.getNodeType().equals(1)
                    || nodeData.getNodeType().equals(0));
                node.setType(nodeData.getNodeType());
                nodes.add(node);
            }
            
        }
        
        return nodes;
        
    }
    
    @Override
    public TreeNodeData add(TreeNodeData node)
    {
        
        return dao.save(node);
    }
}
